/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.dyw.ui;

import cn.dyw.player.Player;
import cn.dyw.player.PlayerFactory;
import cn.dyw.ui.component.CircleButton;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Objects;

public class MainPanel {
    private JPanel musicPanel;
    private JLabel musicImage;
    private JButton next;
    private JButton stop;
    private JButton previous;
    private JProgressBar musicProgress;
    private JPanel operationPanel;
    private JLabel musicTitle;

    private Player player = PlayerFactory.factory();

    private int musicPlayerStatus = 0;

    public JPanel getMusicPanel() {
        return musicPanel;
    }

    public JLabel getMusicImage() {
        return musicImage;
    }

    public JButton getNext() {
        return next;
    }

    public JButton getStop() {
        return stop;
    }

    public JButton getPrevious() {
        return previous;
    }

    public JProgressBar getMusicProgress() {
        return musicProgress;
    }

    public JPanel getOperationPanel() {
        return operationPanel;
    }

    public JLabel getMusicTitle() {
        return musicTitle;
    }


    private void createUIComponents() {
        try {
            musicImage = new JLabel(createIcon("default.png", 300, 300));

            next = new CircleButton(createIcon("快进.png", 20, 20));

            previous = new CircleButton(createIcon("快退.png", 20, 20));

            stop = new CircleButton(createIcon("bofang_1.png", 20, 20));

            musicTitle = new JLabel();

            stop.addActionListener(new ActionListener() {
                /**
                 * Invoked when an action occurs.
                 *
                 * @param e
                 */
                @Override
                public void actionPerformed(ActionEvent e) {
                    switch (musicPlayerStatus) {
                        case 0:
                            if (!player.isReady()) {
                                JOptionPane.showMessageDialog(musicPanel, "请先选择播放文件", "播放错误", JOptionPane.ERROR_MESSAGE);
                            } else {
                                // 当前是停止，开始播放
                                switchStopButtonIcon(1);
                                player.play();
                            }
                            break;
                        case 1:
                            // 当前是播放，停止播放
                            switchStopButtonIcon(0);
                            player.suspend();
                            break;
                    }
                }
            });
        } catch (Exception e) {
            System.out.println("构建初始化出错");
            System.exit(1);
        }
    }


    /**
     * 从类路径下加载图片
     *
     * @param url 图片路径
     * @param x   放缩宽度
     * @param y   放缩高度
     * @return 图片
     * @throws IOException IOException
     */
    public static Icon createIcon(String url, int x, int y) throws IOException {
        Icon icon = null;
        Image image = ImageIO.read(Objects.requireNonNull(MainPanel.class.getClassLoader().getResource(url)));
        icon = new ImageIcon(Objects.requireNonNull(image.getScaledInstance(x, y, Image.SCALE_DEFAULT)));
        return icon;
    }

    /**
     * 从类路径下加载图片
     *
     * @param imageIcon 图片
     * @param x         放缩宽度
     * @param y         放缩高度
     * @return 图片
     */
    public static Icon createIcon(ImageIcon imageIcon, int x, int y) {
        return new ImageIcon(Objects.requireNonNull(imageIcon.getImage().getScaledInstance(x, y, Image.SCALE_DEFAULT)));
    }

    /**
     * 切换图标
     *
     * @param status 0 切换成停止转台 1 切换成播放状态
     */
    public void switchStopButtonIcon(int status) {
        try {
            if (status == 0) {
                this.musicPlayerStatus = 0;
                stop.setIcon(createIcon("bofang_1.png", 20, 20));
            } else if (status == 1) {
                this.musicPlayerStatus = 1;
                stop.setIcon(createIcon("zantingtingzhi.png", 20, 20));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 重置进度条
     */
    public void resetProgress() {
        this.getMusicProgress().setValue(0);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        musicPanel = new JPanel();
        musicPanel.setLayout(new BorderLayout(0, 0));
        musicImage.setHorizontalAlignment(0);
        musicImage.setHorizontalTextPosition(11);
        musicImage.setText("");
        musicPanel.add(musicImage, BorderLayout.NORTH);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        musicPanel.add(panel1, BorderLayout.SOUTH);
        operationPanel = new JPanel();
        operationPanel.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(operationPanel, BorderLayout.CENTER);
        previous.setText("");
        operationPanel.add(previous, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        stop.setText("");
        operationPanel.add(stop, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        next.setHorizontalTextPosition(0);
        next.setText("");
        operationPanel.add(next, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        musicProgress = new JProgressBar();
        panel1.add(musicProgress, BorderLayout.NORTH);
        musicTitle.setEnabled(true);
        Font musicTitleFont = this.$$$getFont$$$(null, -1, 22, musicTitle.getFont());
        if (musicTitleFont != null) musicTitle.setFont(musicTitleFont);
        musicTitle.setHorizontalAlignment(0);
        musicTitle.setText("");
        musicPanel.add(musicTitle, BorderLayout.CENTER);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        return new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return musicPanel;
    }

}
